!(function (factory) {
    'use strict';
    'function' === typeof define && define.amd ? define(['jquery'], factory) : factory(jQuery);
})(function ($) {

    var Select = {
        template: '<option {{selected}} value="{{key}}">{{value}}</option>',
        init: function (opt) {
            var self = this;
            var select = opt.select || [];
            if (opt.data) {
                Select.setContent.call(self, opt.data, select, opt);
            } else {
                $.ajax({
                    type: opt.type,
                    url: opt.url
                }).success(function (data) {
                    Select.setContent.call(self, data, select, opt);
                });
            }
        },
        setContent: function (data, select, opt) {
            var tmp;
            var self = this;
            if ($.isArray(data)) {
                tmp = Select.template;
                var optionStr = '';
                $.each(data, function (index, item) {
                    if (item[opt.structure.key] && item[opt.structure.value]) {
                        optionStr += tmp.replace(/{{key}}/, item[opt.structure.key])
                            .replace(/{{value}}/, item[opt.structure.value])
                            .replace(/{{selected}}/, item[opt.structure.selected] ? 'selected="selected"' : '');
                    }
                });
                '' !== optionStr.trim() && $(self).append(optionStr);
                Select.setSelected.call(self, select);
                $.isFunction(opt.success) && opt.success.call(self, data);
                // 触发事件
                $.Event('xumf.select.success', {relatedTarget: self[0]});
                $(self).trigger('xumf.select.success', [data]);
            }
        },
        setSelected: function (select) {
            var $this = $(this);
            select.forEach(function (obj) {
                $this.find('option').each(function (index, item) {
                    if ($this.attr('multiple')) {
                        if ($(item).val() === obj.value) {
                            $(item).attr('selected', obj.selected);
                        }
                    } else {
                        if ($(item).val() === obj.value) {
                            $(item).attr('selected', obj.selected);
                        } else {
                            if (obj.selected) {
                                $(item).attr('selected', false);
                            }
                        }
                    }
                });
            });
        }
    };

    $.fn.xumf_select = $.fn.xumfSelect = function (opt) {

        if (!$(this).is('select')) {
            return this;
        }
        if ('string' === typeof opt) {
            // 获取方法
            var method = Select[opt];
            // 判断方法存不存在
            if (!method) throw"xumf_select - 没有这样的方法: " + opt;
            // 获取参数
            var args = $.makeArray(arguments).slice(1);
            // 执行方法
            return method.apply(this, args)
        }
        return this.each(function () {
            opt = $.extend({}, $.fn.xumf_select.defaluts, opt || {});
            Select.init.call(this, opt);
        });
    };

    $.fn.xumf_select.defaluts = $.fn.xumfSelect = {
        url: null,
        type: 'GET',
        success: null,
        // 返回值结构
        structure: {
            key: 'value',
            value: 'label',
            selected: 'selected'
        }
    };
});